<template>
  <view class="container">
    <!-- 加载状态 -->
    <view v-if="loading" class="loading-container">
      <view class="native-spinner">
        <view class="spinner-dot"></view>
        <view class="spinner-dot"></view>
        <view class="spinner-dot"></view>
      </view>
      <text class="loading-text">加载中...</text>
    </view>

    <!-- 空状态 -->
    <view v-else-if="leaveQuotas.length === 0" class="empty-state">
      <view class="empty-icon">
        <text class="iconfont icon-empty"></text>
      </view>
      <text class="empty-text">暂无可休假期信息</text>
      <button class="refresh-btn" @click="loadAvailableLeaves">
        <text class="iconfont icon-refresh"></text>
        <text>刷新</text>
      </button>
    </view>

    <!-- 假期列表 -->
    <view v-else class="leave-list">
      <view
          v-for="(quota, index) in leaveQuotas"
          :key="quota.leaveTypeId || index"
          class="leave-card"
      >
        <!-- 卡片头部 -->
        <view class="card-header">
          <view class="type-info">
            <text class="type-name">{{ quota.leaveTypes.typeName || '未知类型' }}</text>
            <text
                class="type-tag"
                :class="quota.leaveTypes && quota.leaveTypes.isPaid ? 'paid-tag' : 'unpaid-tag'"
            >
              {{ quota.leaveTypes && quota.leaveTypes.isPaid ? '带薪' : '无薪' }}
            </text>
          </view>

          <!-- 进度条 -->
<!--          <view class="progress-container">-->
<!--            <view-->
<!--                class="progress-bar"-->
<!--                :style="{ width: progressPercent(quota) + '%' }"-->
<!--            ></view>-->
<!--            <text class="progress-text">{{ progressPercent(quota) }}%</text>-->
<!--          </view>-->
        </view>

        <!-- 卡片内容 -->
        <view class="card-body">
          <view class="quota-row">
            <text class="quota-label">总假期:</text>
            <text class="quota-value">{{ quota.daysAvailable || 0 }} 天</text>
          </view>

          <view class="quota-row">
            <text class="quota-label">已使用:</text>
            <text class="quota-value used-value">{{ quota.daysUsed || 0 }} 天</text>
          </view>

          <view class="quota-row remaining-row">
            <text class="quota-label">剩余额度:</text>
            <text class="quota-value remaining-value">{{ quota.daysRemaining || 0 }} 天</text>
          </view>
        </view>

        <!-- 卡片底部 -->
        <view class="card-footer">
          <text class="leave-desc">{{ quota.leaveTypes && quota.leaveTypes.description || '暂无描述' }}</text>
          <button
              class="apply-btn"
              @click="goToApply(quota.id)"
          >
            申请
          </button>
        </view>
      </view>
    </view>

    <!-- 页面底部 -->
    <view class="page-footer">
      <text class="footer-text">上次更新: {{ lastUpdateTime }}</text>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      loading: true,
      leaveQuotas: [],
      lastUpdateTime: ''
    }
  },
  onLoad() {
    // 使用模拟数据
    setTimeout(() => {
      this.loadMockData()
    }, 800) // 模拟加载延迟
  },
  methods: {
    // 加载模拟数据
    loadMockData() {
      this.loading = true
      this.req({
        url: '/leave/quotas/list',
        method: 'POST',
        data: {}
      }).then(res => {
        this.leaveQuotas  = res.rows
        // this.total = res.total;
        // this.loading = false;
        // this.loadStatus = this.page * this.pageSize >= this.total ? 'nomore' : 'more';
        this.loading = false
      })
    },

    // 格式化日期时间
    formatDateTime(date) {
      const year = date.getFullYear()
      const month = String(date.getMonth() + 1).padStart(2, '0')
      const day = String(date.getDate()).padStart(2, '0')
      const hours = String(date.getHours()).padStart(2, '0')
      const minutes = String(date.getMinutes()).padStart(2, '0')
      return `${year}-${month}-${day} ${hours}:${minutes}`
    },

    // 前往申请特定类型假期
    goToApply(id) {
      uni.navigateTo({
        url: `/pages/work/leave/apply-leave?id=${id}`
      })
    },

    // 获取假期类型名称
    getLeaveTypeName(leaveTypeId) {
      const quota = this.leaveQuotas.find(item => item.leaveTypeId === leaveTypeId)
      return quota ? quota.typeName : '假期'
    },

    // 计算使用百分比
    progressPercent(quota) {
      if (!quota.daysAvailable || quota.daysAvailable <= 0) return 0
      const daysUsed = quota.daysUsed || 0
      const percent = Math.round((daysUsed / quota.daysAvailable) * 100)
      return Math.min(100, Math.max(0, percent))
    }
  }
}
</script>

<style>
/* 基础样式 */
.container {
  padding: 0;
  background-color: #f7f7f7;
  min-height: 100vh;
}

/* 页面头部样式 */
.page-header {
  padding: 40rpx 0;
  background-color: #1890ff;
  text-align: center;
}

.page-title {
  font-size: 36rpx;
  font-weight: bold;
  color: #fff;
}

/* 加载状态样式 */
.loading-container {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  height: 400rpx;
}

.native-spinner {
  display: flex;
  justify-content: center;
  align-items: center;
  margin-bottom: 20rpx;
}

.spinner-dot {
  width: 20rpx;
  height: 20rpx;
  background-color: #1890ff;
  border-radius: 50%;
  margin: 0 10rpx;
  animation: spinner 1.4s infinite ease-in-out both;
}

.spinner-dot:nth-child(1) {
  animation-delay: -0.32s;
}

.spinner-dot:nth-child(2) {
  animation-delay: -0.16s;
}

.loading-text {
  font-size: 28rpx;
  color: #666;
}

@keyframes spinner {
  0%, 80%, 100% { transform: scale(0); }
  40% { transform: scale(1); }
}

/* 空状态样式 */
.empty-state {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  height: 500rpx;
}

.empty-icon {
  font-size: 120rpx;
  color: #cccccc;
  margin-bottom: 20rpx;
}

.empty-text {
  font-size: 28rpx;
  color: #999;
  margin-bottom: 40rpx;
}

.refresh-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 200rpx;
  height: 72rpx;
  border: 1rpx solid #1890ff;
  border-radius: 36rpx;
  color: #1890ff;
  font-size: 28rpx;
}

.refresh-btn .iconfont {
  margin-right: 10rpx;
}

/* 假期列表样式 */
.leave-list {
  padding: 24rpx;
}

.leave-card {
  background-color: #fff;
  border-radius: 16rpx;
  margin-bottom: 24rpx;
  box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.05);
  overflow: hidden;
  transition: transform 0.2s;
}

.leave-card:active {
  transform: scale(0.995);
}

.card-header {
  padding: 28rpx 32rpx;
  border-bottom: 1rpx solid #f5f5f5;
  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: #fafafa;
}

.type-info {
  display: flex;
  align-items: center;
}

.type-name {
  font-size: 36rpx;
  font-weight: 600;
  color: #333;
  margin-right: 16rpx;
}

.type-tag {
  font-size: 24rpx;
  padding: 6rpx 16rpx;
  border-radius: 20rpx;
  font-weight: 500;
}

.paid-tag {
  background-color: #e6f7ff;
  color: #1890ff;
}

.unpaid-tag {
  background-color: #fff2e8;
  color: #fa8c16;
}

.progress-container {
  width: 160rpx;
  height: 24rpx;
  background-color: #f5f5f5;
  border-radius: 12rpx;
  position: relative;
}

.progress-bar {
  height: 100%;
  border-radius: 12rpx;
  background: linear-gradient(90deg, #1890ff 0%, #40a9ff 100%);
}

.progress-text {
  position: absolute;
  right: -50rpx;
  top: 0;
  font-size: 24rpx;
  color: #666;
  font-weight: 500;
}

.card-body {
  padding: 32rpx;
}

.quota-row {
  display: flex;
  margin-bottom: 24rpx;
  align-items: center;
}

.quota-row:last-child {
  margin-bottom: 0;
}

.quota-label {
  font-size: 28rpx;
  color: #666;
  width: 200rpx;
}

.quota-value {
  font-size: 30rpx;
  color: #333;
  font-weight: 500;
}

.used-value {
  color: #ff7a45;
}

.remaining-row {
  margin-top: 28rpx;
  padding-top: 28rpx;
  border-top: 1rpx dashed #eee;
}

.remaining-value {
  color: #1890ff;
  font-weight: 600;
  font-size: 32rpx;
}

.card-footer {
  padding: 28rpx 32rpx;
  border-top: 1rpx solid #f5f5f5;
  display: flex;
  justify-content: space-between;
  align-items: center;
  background-color: #fafafa;
}

.leave-desc {
  font-size: 26rpx;
  color: #888;
  max-width: 60%;
  line-height: 1.5;
}

.apply-btn {
  width: 180rpx;
  height: 72rpx;
  margin-right: 40rpx;
  line-height: 72rpx;
  border-radius: 36rpx;
  font-size: 28rpx;
  border: none;
  background: linear-gradient(135deg, #1890ff 0%, #40a9ff 100%);
  color: #fff;
  font-weight: 500;
  box-shadow: 0 4rpx 12rpx rgba(24, 144, 255, 0.2);
}

.apply-btn:active {
  background: linear-gradient(135deg, #096dd9 0%, #1890ff 100%);
  box-shadow: 0 2rpx 6rpx rgba(24, 144, 255, 0.3);
}

/* 页面底部样式 */
.page-footer {
  padding: 20rpx 0;
  text-align: center;
  color: #999;
  font-size: 24rpx;
}

.footer-text {
  opacity: 0.7;
}

/* 图标字体样式 */
@font-face {
  font-family: 'iconfont';
  src: url('https://at.alicdn.com/t/font_1777785_rz47f7935n.ttf') format('truetype');
}

.iconfont {
  font-family: "iconfont" !important;
  font-size: 16px;
  font-style: normal;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

.icon-empty:before {
  content: "\e60a";
}

.icon-refresh:before {
  content: "\e614";
}
</style>



